# from flask import Flask
#
# app = Flask(__name__)
#
#
# @app.route('/')
# def hello_world():  # put application's code here
#     return 'Hello World!'
#
#
# if __name__ == '__main__':
#     app.run()
import cv2
import base64
from flask import Flask, jsonify

app = Flask(__name__)

def capture_image():
    cap = cv2.VideoCapture(0)  # 打开默认相机
    ret, frame = cap.read()
    cap.release()
    if ret:
        return frame  # 返回NumPy数组（H×W×3，BGR格式）
    return None

@app.route('/capture')
def get_image():
    image = capture_image()
    if image is not None:
        # 转换为PNG字节流并编码为Base64
        _, buffer = cv2.imencode('.png', image)
        img_base64 = base64.b64encode(buffer).decode('utf-8')
        return jsonify({"image": img_base64})
    else:
        return jsonify({"error": "无法捕获图像"}), 500

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5000)